var mmType = 0;
var mmSort = 0;
//默认加载
function loadData(pageNo){
	var shopId = getvl("shopId");
	var search = getvl("search");
	var urladd = "/shop/get/all?pageNum="+pageNo+"&pageSize="+pageSize;
	if(mmType != 0){
		urladd += "&type="+mmType;
	}
	if(mmSort !=0){
		urladd += "&sort="+mmSort;
	}
	if(shopId !=null && shopId != undefined){
		urladd += "&shopId="+shopId;
	}
	if(search !=null && search != undefined){
		urladd += "&search="+search;
	}
	$.ajax({
		url: urlcore + urladd,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if(pageNo == 1){
				$('#goodsList').html("");
			}
			nextPage = data.data.nextPage;
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){
					var strHtml = 
						'<div class="fl m_service_tab_service">'+
				   		'	<dl>'+
				   		'		<a href="4.1.0shangcheng_shangpin_detail.html?goodsId='+n.id+'">'+
					   	'			<dt style="background-image:url('+n.coverUrl+');"></dt>'+
					   	'			<dd class="m_shangcheng_dd">'+
					    '    			<h6>'+n.simpleIntroduce+'</h6>'+
					    '    			<div class="clearfix">'+
					    '    				<h4>&yen;<em>'+n.realPrice+'</em></h4>'+
					    '    				<span>已售'+n.sales+'</span>'+
					    '    			</div>'+
					    '    		</dd>'+
				      	'		</a>'+
				      	'	</dl>'+
				      	'</div>';	
					$('#goodsList').append(strHtml);
				});							
			} else if (data.code == 'OVERTIME'){
				alert("登录超时！");
			} else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

/**
 * 获取商品详情
 * @param {Object} goodsId 商品id
 */
function oneGoodsDetail(goodsId){
	$.ajax({
		url: urlcore+"/shop/get/detail?id="+goodsId,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if(data.success == true){
				var n = data.data;
			
			//分享设置头标题
			jumeiLogoUrl = n.goodsDetail.coverUrl;
			setTitle(n.goodsDetail.simpleIntroduce);
			wxShare(strShareUrl,n.goodsDetail.simpleIntroduce);
			
			//商品详情banner滚动图
			$("#goodsBanner").html("");
			var bannerImgs = n.goodsDetail.goodsUrl.split("***");
			for (var i = 0; i < bannerImgs.length; i++) {
				$("#goodsBanner").append('<div class="swiper-slide"><a href="#"><img src="'+bannerImgs[i]+'"></a></div>');
			}
		    var swiper = new Swiper('.swiper-container', {
		        pagination: '.swiper-pagination',
		        paginationClickable: true,
		        spaceBetween: 30,
		        centeredSlides: true,
		        autoplay: 2500,
		        autoplayDisableOnInteraction: false
		    });
		    
			//商品简介
			var strHtml = 
				'<h2>'+n.goodsDetail.simpleIntroduce+'</h2>'+
				'<div>'+
				'	<span>&yen;'+n.goodsDetail.realPrice+'</span>'+
				'	<em>原价：&yen;'+n.goodsDetail.originalPrice+'</em>'+
				'	<i>已售'+n.goodsDetail.sales+'</i>'+
				'</div>';
			$("#goodsIntroduce").html(strHtml);
			//评论简介
			if(n.comments != null){
				strHtml = 
					'<div class="m_detail_pingjia_title">'+
					'	<a href="2.2.6sheyingshi_fuwu_detail_pingjia.html?servicesGoodsType=1&allId='+n.goodsDetail.id+'"><span>'+n.commentSize+'</span>条评价</a>'+
					'</div>'+
					'<div class="m_detail_pingjia_main">'+
					'	<dl class="pel-list">'+
					'		<dt>'+
					'			<img src="'+n.comments.headImgurl+'">'+
					'		</dt>'+
					'		<dd>'+
					'			<div class="clearfix">'+
					'				<h5 class="fl">'+n.comments.nickName+'</h5>'+
					'				<div class="m_service_ziliao_star fr">';
				for (var i = 0; i < n.comments.serviceLevel; i++) {
					strHtml += '<i class="m_curr"></i>';
				}
				for (var i = 0; i < 5-n.comments.serviceLevel; i++) {
					strHtml += '<i></i>';
				}
				strHtml += 
					'				<em>'+n.comments.serviceLevel+'.0分</em>'+			
					'				</div>'+
					'			</div>'+
					'			<span>'+new Date(n.comments.gmtDatetime).pattern("yyyy-MM-dd HH:mm")+'</span>'+
					'		</dd>'+
					'	</dl>'+
					'	<p>'+n.comments.comment+'</p>'+
					'</div>';
			}else{
				strHtml = 
					'<div class="m_detail_pingjia_title">'+
					'	<a href="#">暂无评价</a>'+
					'</div>';
			}
			
			$("#pingjiaSimple").html(strHtml);
			
			if(n.shopDetail != null){
			
			//商品简介
			if(n.shopDetail.sellType == 1){
				strHtml='<a href="4.3.4shangjia_shangpin.html?shopId='+n.shopDetail.id+'&type=1">';
			}else{
				strHtml='<a href="4.3.5shangjia_shangpin_youhuiquan.html?shopId='+n.shopDetail.id+'&type=1">';
			}
			strHtml +=
					'	<dl class="pel-list">'+
					'		<dt>'+
					'			<img src="'+n.shopDetail.brandUrl+'">'+
					'		</dt>'+
					'		<dd>'+
					'			<h5>'+n.shopDetail.shopName+'</h5>'+
					'			<div class="clearfix m_detail_shang_star">'+
					'				<div class="m_service_ziliao_star fl">';
					for (var i = 0; i < n.shopDetail.shopGrade; i++) {
						strHtml += '<i class="m_curr"></i>';
					}
					for (var i = 0; i < 5-n.shopDetail.shopGrade; i++) {
						strHtml += '<i></i>';
					}
			strHtml +=
					'				</div>'+
					'			</div>'+
					'		</dd>'+
					'	</dl>'+
					'</a>';
				$("#shopDetail").html(strHtml);
			}
				
				$("#goodsDetail").html("");
				var detailImgs = n.goodsDetail.detailUrl.split("***");
				for (var i = 0; i < detailImgs.length; i++) {
					$("#goodsDetail").append('<img src="'+detailImgs[i]+'">');
				};
				$("#goodsDetail").append(n.goodsDetail.detailIntroduce);
				
				//单价
				strHtml = 
						'<li class="detail_tuwen">'+
		                '    <dl>'+
		                '        <dt style="background:url('+n.goodsDetail.coverUrl+') no-repeat; background-size:100%;"></dt>'+
		                '        <dd>';
		            
		        if(n.unitPriceList.length >0){
		      			  strHtml +=
		                '            <h6>'+n.unitPriceList[0].unitTitle+'</h6>';
		               }
		                $.each(data.data.unitPriceList,function(i,n){
		                	if(i==0){
		                		strHtml += '<span>&yen;'+n.unitPrice+'</span>';
		                	}else{
		                		strHtml += '<span style="display: none;">&yen;'+n.unitPrice+'</span>';
		                	}		                	
		                });
		        strHtml +=
		                '        </dd>'+
		                '    </dl>'+
		                '</li>'+
		                '<li class="detail_jing">'+
		                '    <h5>'+n.unitPriceList[0].unitName+'</h5>'+
		                '    <div class="clearfix">';
		                $.each(data.data.unitPriceList,function(i,n){
		                	if(i==0){
		                		strHtml += '<span class="curr" class-IDs="'+i+'" id="'+n.id+'">'+n.unitStandard+'</span>';
		                	}else{
		                		strHtml += '<span class-IDs="'+i+'" id="'+n.id+'">'+n.unitStandard+'</span>';
		                	}		            	
		                });
				strHtml +=
		                '    </div>'+
		                '</li>'+
		                '<li class="detail_shuliang clearfix">'+
		                '    <h6>数量</h6>'+
		                '    <div class="fr">'+
		                '        <div class="lt-rt">'+
		                '            <input type="button" class="minus"  value="-">'+
		                '            <input type="text" class="result" id="resultNum" value="1">'+
		                '            <input type="button" class="add" value="+">'+
		                '        </div>'+
		                '    </div>'+
		                '</li>'+
		                '<li class="detail_button clearfix">'+
		                '    <input type="button" class="addshop" value="加入购物车" onclick="createOrder(-2)"/>'+
		                '    <input type="button" value="立即购买" onclick="createOrder(-1)"/>'+
		                '</li>';
		        $("#unitId").html(strHtml);
		        if(n.shopDetail !=null){
		        //商家电话
		        $("#shopPhone").attr("href","tel:"+n.shopDetail.shopPhone)
		        }
		        //净含量
		        $(document).ready(function(){                      
			        $(".detail_jing span").click(function () {
			            $(this).addClass("curr").siblings().removeClass("curr");
			            var ssId = $(this).attr("class-IDs");
			            $(".detail_tuwen span:eq("+ssId+")").css("display","block").siblings("span").css("display","none");
			        }); 
			    });
			    // 数量
			    $(function(){ 
			        $(".add").click(function(){
			            var t=$(this).parent().find('input[class*=result]'); 
			            t.val(parseInt(t.val())+1);
			        })
			        $(".minus").click(function(){ 
			            var t=$(this).parent().find('input[class*=result]'); 
			            t.val(parseInt(t.val())-1);
			            if(parseInt(t.val())<1){ 
			                t.val(1); 
			            } 
			        })
			    }) 
			}else{
				alert(data.msg);
			}			
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

/**
 * 生成订单
 * @param {Object} type -2表示生成订单，加入购物车，-1表示直接购买,临时订单
 */
function createOrder(type){
	var resultNum = $("#resultNum").val();
	if(resultNum <=0){
		toshare();
	}else{
		var goodsId = getvl("goodsId"); //商品id
		var unitId = $(".detail_jing .curr").attr("id");
		$.ajax({
		url: urlcore + "/shop/create/order?goodsId="+goodsId+"&unitId="+unitId+"&orderMount="+resultNum+"&typeStatus="+type,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if(data.success==true){
				if(type==-2){
					alert("加入购物车成功！");
				}else{
					location.href="4.1.3Order_fill.html?orderId="+data.data;
				}
			} else if (data.code == 'OVERTIME'){
				alert("登录超时！");
			} else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
	}
}


/**
 * 初始化订单信息
 */
function initOrderGoods(){
	var orderId = getvl("orderId");
	var myCouponId = getvl("myCouponId");
	if(myCouponId == "" || myCouponId == undefined){
		myCouponId = 0;
	}
	$.ajax({
		url: urlcore + "/shop/get/order/goods/detail?orderId="+orderId+"&myCouponId="+myCouponId,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			var n = data.data;
			if(data.success==true){
				//店家名字
				var strHtml = '<img src="img/fangzi.png"/>'+n.shopName;				
				$("#shopName").html(strHtml);	
				//商品信息
				strHtml = 
					'<img class="commodity_img" src="'+n.goods.coverUrl+'"/>'+
					'<div class="commodity_infor">'+
					'	<div class="commodity_title">'+n.goods.simpleIntroduce+'</div>'+
					'	<div class="commodity_unitprice">'+
					'		<div class="commodity_money">￥'+n.unit.unitPrice+'</div>'+
					'		<div class="commodity_num">×'+n.orderGoods.orderMount+'</div>'+
					'	</div>'+
					'</div>';
				$("#goodsMsg").html(strHtml);
				//优惠券选择
				if(n.userCoupon == undefined){							
					$("#addCouponMsg").append('<div class="coupon_use" id="couponMsg" coupon-id="0">无优惠券</div>');
				}else{
					$("#addCouponMsg").append('<div class="coupon_use" id="couponMsg" coupon-id="'+n.userCoupon.id+'">满'+n.userCoupon.couponLimit+'减'+n.userCoupon.couponSave+'元</div>');
				}
				
				//配送方式
				$("#shippingMethod").html(typeToShippingMethod(n.goods.shippingMethod));
				//订单金额
				strHtml =
					'<div class="coupon_allmoney">合计：<span>￥'+n.orderGoods.orderMoney+'</span></div>'+
					'<div class="coupon_number">共计<span>'+n.orderGoods.orderMount+'</span>件商品</div>';
				$("#orderMoney").html(strHtml);
				//费用详情
				strHtml = 
					'<div class="cost_list">'+
					'	<div class="cost_name">商品总金额</div>'+
					'	<div class="cost_number">￥'+n.orderGoods.orderMoney+'</div>'+
					'</div>'+
					'<div class="cost_list">'+
					'	<div class="cost_name">优惠券抵扣</div>';
				if(n.userCoupon == undefined){
					strHtml += '<div class="cost_number">-￥0</div>';
				}else{
					strHtml += '<div class="cost_number">-￥'+n.userCoupon.couponSave+'</div>';
				}
				strHtml += 
					'</div>'+		
					'<div class="cost_list">'+
					'	<div class="cost_name">订单运费</div>'+
					'	<div class="cost_number">+￥'+n.goods.shippingMoney+'</div>'+
					'</div>';
				$("#costDetail").html(strHtml);
				

				$("#payButton").html("去支付（￥"+n.needPayMoney+"元）");
				
			} else if (data.code == 'OVERTIME'){
				alert("登录超时！");
			} else {
				alert(data.msg);
			}
		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});
}

/**
 * 跳转商品订单付款页面
 */
function gotoGoodsPayHtml(){	
	var orderId = getvl("orderId");
	var addressId = $("#addressId").attr("address-id");
	var couponId = $("#couponMsg").attr("coupon-id");
	if(couponId == null || couponId == undefined){
		couponId = 0;
	}
	if(!checkIsEmpty(addressId,"地址信息未填写！")){
		return false;
	}
	var remark = $("#leaving").val();

	$.ajax({
		url: urlcore+"/shop/goto/pay/order",
		type: "post",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		data:JSON.stringify({
			"id":orderId,
			"addressId":addressId,
			"userCouponId":couponId,
			"remark":remark
		}),
		success:function(data){	
			if(data.success == true){
				location.href = "4.2.0zhifu.html?orderId="+orderId+"&orderType="+data.data;
			}else{
				alert(data.msg);
			}
		},
		error:function() {
			alert("error");
		}
	});		

}

/**
 * 初始化订单金额
 */
function initGoodsOrder(){
	var orderId = getvl("orderId");
	$.ajax({
		url: urlcore+"/shop/get/goods/order?orderId="+orderId,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){	
			if(data.success == true){
				$("#orderNumber").html("订单编号: "+data.data.orderNumber);
				$("#orderMoney").html("&yen;"+data.data.payMoney);
				$("#payButton").val("确认支付（"+data.data.payMoney+"元）");
			}else{
				alert(data.msg);
			}
		},
		error:function() {
			alert("error");
		}
	});		
}

/**
 * 初始化成功页面
 */
function initSuccessHtml(){
	var orderId = getvl("orderId");
	$.ajax({
		url: urlcore+"/shop/get/goods/order?orderId="+orderId,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){		
			$("#orderNum").html(data.data.orderNumber);
			$("#payType").html(typeToPayName(data.data.payType));
			$("#payMoney").html("&yen;"+data.data.payMoney);
			$("#payTime").html(new Date(data.data.uptDatetime).pattern("yyyy-MM-dd HH:mm:ss"));
		},
		error:function() {
			alert("error");
		}
	});
}

/**
 * 初始化店铺信息
 */
function initShopMsg(){
	var	shopId = getvl("shopId");
	$.ajax({
		url: urlcore + "/shop/get/shop/by/id?shopId="+shopId,
		type: "get",
		dataType: 'jsonp',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			var n = data.data;
			//分享设置头标题
			jumeiLogoUrl = n.brandUrl;
			setTitle(n.shopName);
			wxShare(strShareUrl,n.shopName);
			
			var strHtml = 
					'<div class="m_service_ziliao_img">'+
					'	<img src="'+n.brandUrl+'">'+
					'</div>'+
					'<h4>'+n.shopName+'</h4>';
			if(n.sellType == 1){
				strHtml += '<h6>销量 '+n.salesNumber+'共'+n.goodsSorts+'件宝贝</h6>';
			}else{
				strHtml += '<h6>销量 '+n.couponsNumber+'共'+n.couponsSorts+'件宝贝</h6>';
			}
				strHtml +=
					'<p><span>'+n.shopIntroduce+'</span></p>';
			$("#shopMsg").html(strHtml);
			$("#shopCoverUrl").attr("src",n.coverUrl);
			$("#shopIntroduce").html(n.detailIntroduce);
		},
		error:function() {
			alert("error");
		}
	});

}